package com.itlong.cloud.commons.service.impl;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.VO.property.PropertyVisitorInfoByPushVO;
import com.itlong.cloud.commons.dao.IVisitorPushMessageDao;
import com.itlong.cloud.commons.service.IDeviceAlarmPushMessageService;
import com.itlong.cloud.commons.service.IVisitorPushMessageService;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.push.message.GatewayService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 * 告警事件上报后主动向前端推送消息
 * </desc>
 *
 * @createDate 2021/06/03
 */
@Service
public class DeviceAlarmPushMessageServiceImpl implements IDeviceAlarmPushMessageService {

    private static final Logger LOG = LoggerFactory.getLogger(DeviceAlarmPushMessageServiceImpl.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * <desc>
     * 告警事件上报后主动向前端推送消息
     * </desc>
     * @param projectId 项目编号
     *
     * @return
     * @author JuGuang.S
     * @createDate 2021/06/03
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deviceAlarmPushMessage(String projectId,boolean remark,String eventSeq) throws Exception {

        Map<String,String> resultMap = new HashMap<>();
        //remark 为true时则推送消息为实时抓拍 false则为告警新增事件
        if(remark){
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date();
            String createTime = df.format(date);
            resultMap.put("mark","snapshot");
            resultMap.put("time",createTime);
            resultMap.put("alarmId",eventSeq);
        }else{
            resultMap.put("mark","deviceAlarm");
        }
        resultMap.put("projectId",projectId);
        String strUTF8 = URLDecoder.decode(JSON.toJSONString(resultMap), "UTF-8");
        System.out.println("strUTF8:" + strUTF8);
        stringRedisTemplate.convertAndSend(RedisConstant.WEBSOCKET_REDIS_MQ_TOPIC, strUTF8);

    }

}
